package com.DataFactory.TaskManagement.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.DataFactory.TaskManagement.converter.TaskArrangeConverter;
import com.DataFactory.TaskManagement.mapper.TaskArrangementMapper;
import com.DataFactory.TaskManagement.mapper.TaskMapper;
import com.DataFactory.TaskManagement.model.bo.TaskArrangeBO;
import com.DataFactory.TaskManagement.model.entity.TaskArrangement;
import com.DataFactory.TaskManagement.model.form.TaskArrangementForm;
import com.DataFactory.TaskManagement.model.query.TaskArrangementQuery;
import com.DataFactory.TaskManagement.model.vo.TaskArrangementPageVO;
import com.DataFactory.TaskManagement.service.TaskArrangeService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor

public class TaskArrangeServiceImpl extends ServiceImpl<TaskArrangementMapper, TaskArrangement> implements TaskArrangeService {
    @Autowired
    TaskArrangementMapper taskArrangementMapper;
    @Autowired
    TaskMapper taskMapper;
    @Autowired
    private TaskArrangeConverter converter;

    /****
     * 用于按照type分割任务并插入
     * @param form
     * @return
     */
    @Override
    public Boolean addTaskArrangement(TaskArrangementForm form) {
        TaskArrangement task = converter.form2entity(form);
        boolean flag=false;
        String types=task.getType();

        if (types!=null||!types.equals("")){
        //把type按照，分割转换成数组
        String[] typesArr=types.split(",");
        //循环遍历数组新建一个任务安排对象放到数据库
            for (int i=0;i<typesArr.length;i++){
                String type=taskArrangementMapper.selectTypeById(Integer.parseInt(typesArr[i]));
                TaskArrangement taskArrangement1=new TaskArrangement();
                taskArrangement1.setTaskId(2);//TODO id拿不到
                taskArrangement1.setType(type);
                taskArrangement1.setIsArrange(0);
                taskArrangementMapper.insert(taskArrangement1);
            }
            flag=true;
        }
        return flag;
    }

    @Override
    public Boolean updateTaskArrangement(Integer id, TaskArrangementForm taskArrangementForm) {
        TaskArrangement arrangement = converter.form2entity(taskArrangementForm);
        arrangement.setId(id);
        if(arrangement.getDuration()!=null ){
            arrangement.setIsSet(1);
        }
        if(arrangement.getIsArrange()!=null && !arrangement.getIsArrange().equals(0) ){
            arrangement.setIsArrange(1);
        }
        boolean result = this.updateById(arrangement);
        return result;
    }


    /**
     * 批量根据id删除任务
     * @param idsStr
     * @return
     */
    @Override
    @Transactional
    public Boolean deleteTaskArrangments(String idsStr) {
        Assert.isTrue(StrUtil.isNotBlank(idsStr), "删除的用户数据为空");

        List<Long> ids = Arrays.stream(idsStr.split(","))
                .map(Long::parseLong).collect(Collectors.toList());

        boolean deleted = this.removeByIds(ids);
        return deleted;
    }

    @Override
    public Page<TaskArrangementPageVO> getTaskPage(TaskArrangementQuery queryParams) {
        Page<TaskArrangeBO> page = new Page<>(queryParams.getPageNum(), queryParams.getPageSize());
        Page<TaskArrangeBO> boPage =  taskArrangementMapper.getTaskPage(page, queryParams);
        Page<TaskArrangementPageVO> taskArrangePageVOPage = converter.bo2vo((boPage));
        return taskArrangePageVOPage;
    }

    /***
     * 用来直接插入一条安排数据
     * @param taskArrangement
     * @return
     */
    @Override
    public Boolean addTaskArrangement(TaskArrangement taskArrangement) {
        taskArrangementMapper.insert(taskArrangement);
        return true;
    }

    @Override
    public List<TaskArrangement> getList() {
        List<TaskArrangement> taskArrangements = taskArrangementMapper.selectAll();
        return taskArrangements;
    }

    @Override
    public TaskArrangementForm getTaskArrangementForm(Integer id) {
        TaskArrangementForm taskArrangementForm = taskArrangementMapper.getTaskArrangementForm(id);
        return taskArrangementForm;
    }
}
